package com.ihd.ihardware.common.widget;

import android.app.Activity;
import android.app.Dialog;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.ihd.ihardware.R;


public class BindDialog extends Dialog {
    private View.OnClickListener mCancleClickListener;
    private View.OnClickListener mConfirmClickListener;
    private Activity mActivity;
    private int mLayout;
    private String mHead;
    private String mName;

    public BindDialog(Activity activity, int theme, int layout, View.OnClickListener confirmBtnClickListener, View.OnClickListener cancelBtnClickListener) {
        super(activity, theme);
        mActivity = activity;
        mConfirmClickListener = confirmBtnClickListener;
        mCancleClickListener = cancelBtnClickListener;
        mLayout = layout;
    }

    public BindDialog(Activity activity, int theme, int layout, String head, String name, View.OnClickListener confirmBtnClickListener, View.OnClickListener cancelBtnClickListener) {
        super(activity, theme);
        mActivity = activity;
        mConfirmClickListener = confirmBtnClickListener;
        mCancleClickListener = cancelBtnClickListener;
        mLayout = layout;
        mHead = head;
        mName = name;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(mLayout);
        findViewById(R.id.bind).setOnClickListener(mConfirmClickListener);
        findViewById(R.id.unbind).setOnClickListener(mCancleClickListener);
        findViewById(R.id.close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cancel();
            }
        });
        setCanceledOnTouchOutside(false);
        setCancelable(true);
        if (mHead != null && !"".equals(mHead)) {
            Glide.with(mActivity)
                    .load(Uri.parse(mHead))
                    .apply(RequestOptions.circleCropTransform())
                    .into((ImageView) findViewById(R.id.head));
        }
        if (mName != null) {
            ((TextView) findViewById(R.id.name)).setText(mName);
            TranslateAnimation translateAnimation = new TranslateAnimation(0, 0, -40, 60);
            translateAnimation.setRepeatCount(Animation.INFINITE);
            translateAnimation.setRepeatMode(Animation.REVERSE);
            translateAnimation.setDuration(1000);
            findViewById(R.id.indicate).startAnimation(translateAnimation);
        }
    }

}
